//
//  ST_EditDailyMomentImagesView.swift
//  btvideo
//
//  Created by mac on 2020/7/23.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_EditDailyMomentImagesView: UIView {
    
    static let height = (ScreenWidth - 15 * 2 - 11 * 3)/4
    
    fileprivate let baseTag = 102
    
    
    fileprivate lazy var append_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleColor(UIColor.gray, for: UIControl.State.normal)
        control.setImageForAllStates(ST_ImageUtils.imageName("daily_preview_img_append_icon")!)
        control.addTarget(self, action: #selector(appendHandle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    var pictures:[(identifier:String,image:UIImage)]?{
        didSet{
            layout_child_views()
        }
    }
    
    
    fileprivate func layout_child_views(){
        
        self.removeSubviews()
        guard let array = pictures else { return  }
        
        var lastItem:ST_EditDailyMomentImageItemView?
        for (idx,item) in array.enumerated(){
            let itemView = ST_EditDailyMomentImageItemView()
            itemView.picture = item
            itemView.tag = self.baseTag + idx
            
            itemView.addTarget(self, action: #selector(previewImage(sender:)), for: UIControl.Event.touchUpInside)
            
            itemView.clear_closure = {[weak self] picture in

                _ = self?.pictures?.removeFirst(where: { (id,img) -> Bool in
                    return picture.identifier == id
                })
                
                self?.layout_child_views()
                
                self?.deleteClosure?(picture)
            }
            
            self.addSubview(itemView)
            itemView.snp.makeConstraints { (make) in
                if let last = lastItem{
                    make.left.equalTo(last.snp.right).offset(11)
                }else{
                    make.left.equalToSuperview().offset(15)
                }
                make.width.height.equalTo(ST_EditDailyMomentImagesView.height)
                make.top.equalToSuperview()
            }
            lastItem = itemView
        }
        
        if array.count < 4{
            
            self.addSubview(append_btn)
            append_btn.snp.makeConstraints { (make) in
                if let last = lastItem{
                    make.left.equalTo(last.snp.right).offset(11)
                }else{
                    make.left.equalToSuperview().offset(15)
                }
                make.width.height.equalTo(ST_EditDailyMomentImagesView.height)
                make.top.equalToSuperview()
            }
        }
    }
    
    var append_closure:(()->())?
    
    var deleteClosure:(((identifier:String,image:UIImage))->())?
    
}

extension ST_EditDailyMomentImagesView{
    
    
    @objc fileprivate func previewImage(sender:ST_EditDailyMomentImageItemView){
        
        let idx = sender.tag - baseTag
        
        self.previewPicures(index: idx)
        
    }
    
   
    
    @objc fileprivate func appendHandle(){
        
        self.append_closure?()
    }
    
    
    fileprivate func previewPicures(index:Int){
        
               
        guard let data_array = pictures else { return }
        
       var array = [ST_MWPhoto]()
        
       for model in data_array{
           
        guard let media = ST_MWPhoto.init(image: model.image) else{
               continue
           }
           array.append(media)
       }
       
       guard let browser = ST_MWPhotoBrowser.init(photos: array) else{
           return
       }
       browser.displayActionButton = false;
        browser.setCurrentPhotoIndex(UInt(index))
       
       let nav = UINavigationController.init(rootViewController: browser)
       
       GlobalPresent(toController: nav)
        
    }
    
}
